package com.wu.qqserver.service;

import com.wu.qqcommon.Message;
import com.wu.qqcommon.MessageType;
import com.wu.utils.Utility;

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.Date;

public class SendNewsToAllOnLineUserService extends Thread{

    @Override
    public void run() {

        while (true){
            Message message = new Message();
            System.out.print("请输入服务器要推送的信息[exit表示退出推送服务]：");
            String news = Utility.readString(1000);
            if (news.equals("exit")){
                break;
            }
            message.setContent(news);
            message.setSenter("服务器");
            message.setMesType(MessageType.MESSAGE_TO_ALL_MES);
            message.setSentTime(new Date().toString());
            System.out.println("服务器推送消息给所有人说："+news);


            //我们需要获得所有的在线用户的socket
            String[] allUserList = ManageServerConnectClientThread.getOnLineUserList().split(" ");
            for (String user : allUserList) {
                try {
                    ObjectOutputStream oos = new ObjectOutputStream(ManageServerConnectClientThread
                            .getServerConnectClientThread(user)
                            .getSocket()
                            .getOutputStream());
                    oos.writeObject(message);
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }

    }
}
